package com.example.commuinte_backed.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commuinte_backed.common.api.ApiResult;
import com.example.commuinte_backed.model.dto.ChangePasswordDTO;
import com.example.commuinte_backed.model.dto.LoginByEmailDTO;
import com.example.commuinte_backed.model.dto.RegisterDTO;
import com.example.commuinte_backed.model.entity.BmsPost;
import com.example.commuinte_backed.model.entity.UmsUser;
import com.example.commuinte_backed.service.IBmsFollowService;
import com.example.commuinte_backed.service.IBmsPostService;
import com.example.commuinte_backed.service.IUmsUserService;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.example.commuinte_backed.model.dto.LoginByUserNameDTO;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import static com.example.commuinte_backed.jwt.JwtUtil.USER_NAME;


@RestController
@RequestMapping("/ums/user")
public class UmsUserController extends BaseController {
    @Resource
    private IUmsUserService iUmsUserService;

    @Resource
    private IBmsPostService iBmsPostService;

    @Resource
    private IBmsFollowService iBmsFollowService;

   // @RequestMapping(value = "/register", method = RequestMethod.POST)
    @PostMapping("/register")
    public ApiResult<Map<String, Object>> register(@Valid @RequestBody RegisterDTO dto) {
        UmsUser user = iUmsUserService.executeRegister(dto);
        if (ObjectUtils.isEmpty(user)) {
            return ApiResult.failed("账号注册失败");
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("user", user);
        return ApiResult.success(map);
    }
    @RequestMapping(value = "/loginbyusername", method = RequestMethod.POST)
    public ApiResult<Map<String, String>> loginByUserName(@Valid @RequestBody LoginByUserNameDTO dto) {
        String token = iUmsUserService.executeLoginByUserName(dto);
        if (ObjectUtils.isEmpty(token)) {
            return ApiResult.failed("账号密码错误");
        }
        Map<String, String> map = new HashMap<>(16);
        map.put("token", token);
        return ApiResult.success(map, "登录成功");
    }

    @RequestMapping(value = "/loginbyemail", method = RequestMethod.POST)
    public ApiResult<Map<String, String>> loginByEmail(@Valid @RequestBody LoginByEmailDTO dto) {
        String token = iUmsUserService.executeLoginByEmail(dto);
        if (ObjectUtils.isEmpty(token)) {
            return ApiResult.failed("邮箱密码错误");
        }
        Map<String, String> map = new HashMap<>(16);
        map.put("token", token);
        return ApiResult.success(map, "登录成功");

    }
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ApiResult<UmsUser> getUser(@RequestHeader(value = USER_NAME) String userName) {
        UmsUser user = iUmsUserService.getUserByUsername(userName);
        return ApiResult.success(user);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ApiResult<Object> logOut() {
        return ApiResult.success(null, "注销成功");
    }

    @GetMapping("/{username}")
    public ApiResult<Map<String, Object>> getUserByName(@PathVariable("username") String username,
                                                        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> map = new HashMap<>(16);
        UmsUser user = iUmsUserService.getUserByUsername(username);
        Assert.notNull(user, "用户不存在");
        Page<BmsPost> page = iBmsPostService.page(new Page<>(pageNo, size),
                new LambdaQueryWrapper<BmsPost>().eq(BmsPost::getUserId, user.getId()));
        map.put("user", user);
        map.put("topics", page);
        return ApiResult.success(map);
    }
    @PostMapping("/update")
    public ApiResult<UmsUser> updateUser(@RequestBody UmsUser umsUser) {
        iUmsUserService.updateById(umsUser);
        return ApiResult.success(umsUser);
    }

    @PostMapping("/changePassword")
    public ApiResult<String> changePassword(@RequestHeader(value = USER_NAME) String userName,
                                             @Valid @RequestBody ChangePasswordDTO dto) {


        if(iUmsUserService.confirm(userName,dto.getUsedPass())==false){
            return ApiResult.failed("原始密码错误");
        }
        if(iUmsUserService.changePassword(userName,dto)==false){
            return ApiResult.failed("服务器错误");
        }

        return ApiResult.success(null,"修改密码成功");



    }

    @GetMapping("/follow/{username}")
    public ApiResult<Map<String, Object>> getUserByName1(@PathVariable("username") String username,
                                                         @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> map = new HashMap<>(16);
        UmsUser user = iUmsUserService.getUserByUsername(username);
        Assert.notNull(user, "用户不存在");
        Page<UmsUser>page=iBmsFollowService.selectUserByFollowId(new Page<>(pageNo, size),user.getId());
        map.put("user", user);
        map.put("parent", page);
        return ApiResult.success(map);
    }
    @GetMapping("/follower/{username}")
    public ApiResult<Map<String, Object>> getFollower(@PathVariable("username") String username,
                                                         @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> map = new HashMap<>(16);
        UmsUser user = iUmsUserService.getUserByUsername(username);
        Assert.notNull(user, "用户不存在");
        Page<UmsUser>page=iBmsFollowService.selectFollowerByUserId(new Page<>(pageNo, size),user.getId());
        map.put("user", user);
        map.put("follower", page);
        return ApiResult.success(map);
    }




}